package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.InstockTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundInstockEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.Getter;

/**
 * 入库单
 *
 * @author Jian.Wang
 * @version v1.0
 * @date 2019/1/19
 * @since v1.0
 */
@Getter
public enum TempRefundInstockEnum {
    //
    BASE_INFO(RefundInstockEnum.BASE_INFO),
    SHEET_NO(RefundInstockEnum.SHEET_NO),
    TIME(RefundInstockEnum.TIME),
    CUSTOMER_ID(RefundInstockEnum.CUSTOMER_ID),
    REF_ID(RefundInstockEnum.REF_ID),
    WAREHOUSE_ID(RefundInstockEnum.WAREHOUSE_ID),
    PRODUCT_INFO(RefundInstockEnum.PRODUCT_INFO),
    PRODUCT(RefundInstockEnum.PRODUCT),
    PRODUCT_TOTAL(RefundInstockEnum.PRODUCT_TOTAL),
    OTHER_EXPENSE(RefundInstockEnum.OTHER_EXPENSE),
    TOTAL_MONEY(RefundInstockEnum.TOTAL_MONEY),
    EXTEND_INFO(RefundInstockEnum.EXTEND_INFO),
    MANAGER_ID(RefundInstockEnum.MANAGER_ID),
    MEMO(RefundInstockEnum.MEMO),
    SYSTEM_INFO(RefundInstockEnum.SYSTEM_INFO),
    CREATOR_ID(RefundInstockEnum.CREATOR_ID),
    ADD_TIME(RefundInstockEnum.ADD_TIME),
    UPDATE_TIME(RefundInstockEnum.UPDATE_TIME),
    TYPE(RefundInstockEnum.TYPE),
    INCOMING(RefundInstockEnum.INCOMING),
    WAREHOUSE_ID_LINKED_TEXT(RefundInstockEnum.WAREHOUSE_ID_LINKED_TEXT),
    REF_ID_LINKED_TEXT(RefundInstockEnum.REF_ID_LINKED_TEXT),
    CUSTOMER_ID_LINKED_TEXT(RefundInstockEnum.CUSTOMER_ID_LINKED_TEXT),
    ;
    private RefundInstockEnum anEnum;

    TempRefundInstockEnum(RefundInstockEnum refundInstockEnum) {
        this.anEnum = refundInstockEnum;
    }
    public static List<TempRefundInstockEnum> getAllEnum() {
        List<TempRefundInstockEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }
    /**
     * 初始化编号
     *
     */
    public static void initSheetNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.REFUND_INSTOCK.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.REFUND_INSTOCK.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        // 不能重复
        fieldAttrEntity.setNoRepeat(1);
        // 默认的编号规则
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.REFUND_INSTOCK_NO);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化日期
     *
     * @param fieldAttrEntity
     */
    public static void initTime(FieldAttrEntity fieldAttrEntity) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType("today");
        fieldAttrEntity.setDateType(DateTimeEnum.TIME.getType());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }
    /**
     * 初始化关联客户
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initWarehouseId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.WAREHOUSE.getCode());
        fieldAttrEntity.setLinkedText(WAREHOUSE_ID_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化入库类型
     *
     * @author chaoxiong.lei
     * @date  11:01 2019/1/18
     **/
    public static void initType(FieldAttrEntity fieldAttrEntity) {
        // 将出库类型设置为下拉选项
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(InstockTypeEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initRefId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedText(REF_ID_LINKED_TEXT.anEnum.getAttr());
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.REFUND.getCode());
        fieldAttrEntity.setIsMultiBusiness(1);
        fieldAttrEntity.setBusinessType(XbbRefTypeEnum.REFUND_INSTOCK.getCode());
        fieldAttrEntity.setIsForbidden(0);
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(CUSTOMER_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 物流公司
     */
    public static void initExpressNo(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setComboType(ComboTypeEnum.CUSTOM.getType());
        fieldAttrEntity.setDictionaryFlag(0);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initManagerId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setMultiple(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.WAREHOUSE_MANAGER));
    }

    public static void initProducts(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initCustomerId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.CUSTOMER.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_ID_LINKED_TEXT.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initProductTotal(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setMemo("默认值为产品退货单价总计");
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setEditable(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + RefundInstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + RefundInstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "入库产品.单价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
    }

    public static void initOtherExpense(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
    }

    public static void initTotalMoney(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("默认值=产品退货单价合计+其他费用");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + RefundInstockEnum.PRODUCT_TOTAL.getAttr() + "},{self." + RefundInstockEnum.OTHER_EXPENSE.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + RefundInstockEnum.PRODUCT_TOTAL.getAttr(), RefundInstockEnum.PRODUCT_TOTAL.getAttrName());
        labelMap.put("self." + RefundInstockEnum.OTHER_EXPENSE.getAttr(), RefundInstockEnum.OTHER_EXPENSE.getAttrName());
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
    }
}
